package com.serviceImpl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.entity.Article;
import com.entity.Category;
import com.entity.Tag;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.ArticleMapper;
import com.service.ArticleService;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author MingCha Yang
 */
@Service("articleServiceImpl")
public class ArticleServiceImpl implements ArticleService {
	@Resource
	private ArticleMapper articleMapper;

	@Override
	public List<Article> listRecentArticle(Integer limit) {

		return articleMapper.listRecentArticle(limit);
	}

	@Override
	public PageInfo<Article> pagelistArticle(Integer pageNo, Integer pageSize) {
		PageHelper.startPage(pageNo, pageSize);
		List<Article> findAll = articleMapper.findAll();
		PageInfo<Article> pageInfo = new PageInfo<Article>(findAll);
		return pageInfo;
	}
	//为方法加事务,propagation传播方式，isolation隔离级别
	@Override
	//@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
	public void addArticle(Article article) {
		
		int addArticle = articleMapper.addArticle(article);
		//int i = 1/0;//事务回滚了
		for(Category c : article.getCategorys()) {
			articleMapper.addArticleCategory(article.getArticleId(), c.getCategoryId());
		}
		//int i = 1/0;//事务回滚了
		for(Tag t : article.getTags()) {
			
			articleMapper.addArticleTag(article.getArticleId(), t.getTagId());
		}
		//int i = 1/0;//事务回滚了
	}
	@Override
	public Integer getCountsByuserId(Integer userId) {
		
		return articleMapper.getCountsByuserId(userId);
	}


}
